SUFFIXES 	:= .c .cc .cpp

SRCS 		:= $(shell cat sources.list)
INTERFACES 	:= $(shell cat interfaces.list)

TARGET 		:= libAlgebra.a
LIB_DEST_DIR	:= ../lib
INTERFACES_DEST_DIR := ../include

CC 		:= g++
LC 		:= g++

INCLUDE_PATH	:= -I./
CFLAGS		:= -Wall -O

OBJS = $(filter %.o,$(foreach suf,$(SUFFIXES),$(SRCS:$(suf)=.o)))
DEPS = $(filter %.d,$(foreach suf,$(SUFFIXES),$(SRCS:$(suf)=.d)))

default: $(LIB_DEST_DIR) $(INTERFACES_DEST_DIR) $(TARGET)
	@echo "<<<<<< Target " $(TARGET) " generated >>>>>>"

$(TARGET): $(OBJS) $(DEPS)
	@echo "linking $@"
	ar r $(TARGET) $(OBJS)
	cp $(TARGET) $(LIB_DEST_DIR)
	cp $(OBJS) $(LIB_DEST_DIR)
	cp $(INTERFACES) $(INTERFACES_DEST_DIR)

$(LIB_DEST_DIR):
	mkdir $@

$(INTERFACES_DEST_DIR):
	mkdir $@

depend: $(DEPS)
	@echo "subtarget dependecies generated."

%.d : %.cc
	@echo "making dependencies $@"
	@$(CC) -MM -MT "$@ $(@:.d=.o)" $(CFLAGS) $(INCLUDE_PATH) -MF $@ $<

%.d : %.cpp
	@echo "making dependencies $@"
	@$(CC) -MM -MT "$@ $(@:.d=.o)" $(CFLAGS) $(INCLUDE_PATH) -MF $@ $<

%.d : %.c
	@echo "making dependencies $@"
	@$(CC) -MM -MT "$@ $(@:.d=.o)" $(CFLAGS) $(INCLUDE_PATH) -MF $@ $<

%.o : %.cc
	@echo "compiling $<"
	@$(CC) $(CFLAGS) $(INCLUDE_PATH) -c -o $@ $<

%.o : %.c
	@echo "compiling $<"
	@$(CC) $(CFLAGS) $(INCLUDE_PATH) -c -o $@ $<

%.o : %.cpp
	@echo "compiling $<"
	@$(CC) $(CFLAGS) $(INCLUDE_PATH) -c -o $@ $<

clean:
	@echo "removing $(TARGET), .o-files, .d-files"
	@rm -f $(OBJS) $(TARGET) $(DEPS)

clean.d:
	@echo "removing .d-files"
	@rm $(DEPS)

veryclean:
	@echo "removing $(TARGET), .o-files, .d-files $(LIB_DEST_DIR) $(INTERFACES_DEST_DIR)"
	@rm -f $(OBJS) $(TARGET) $(DEPS)
	@rm -f $(LIB_DEST_DIR)/*.a $(LIB_DEST_DIR)/*.o $(INTERFACES_DEST_DIR)/*.h

showsrcs:
	@echo $(SRCS)
	@echo $(INTERFACES)


ifneq ($(MAKECMDGOALS),showsrcs)
  -include $(DEPS)
endif